#ifndef __KEY_H
#define __KEY_H
#include	"main.h"

#define KEY_DEBUG1

#ifdef  KEY_DEBUG
#include "MessageOut.h"
#include "oled.h"
#define  KEY_Printf(fmt,...)   Uprintf(fmt,__VA_ARGS__)
#else
#define  KEY_Printf(...)
#endif

#define  KEY_NUM_MAX    5       //最大能定义的按键数量 

/******按键状态定义******/

#define  KEY_SHORT   (1<<0)     //短按位
#define  KEY_LONG    (1<<1)     //长按位
#define  KEY_MUL     (1<<2)     //短按位
#define  KEY_DOUBLE   (1<<3)    //双击位

/******检测时间定义******/
#define SHORT_MAX_COUNT     35      //短按时间
#define LONG_MAX_COUNT      100     //长按时间
#define MUL_TAP_COUNT       10      //连按间隔时间
#define DOUBLE_MAX_COUNT    30      //双击间隔时间  设为零则关闭双击检测

/******引脚初始化，需要自己添加******/
typedef enum
{
  KEY_RESET = 0U,
  KEY_SET = 1U,
} KeyState;


typedef enum
{
 KEY_CHECK =0U,      //检测按键状态
 KEY_BEGIN=  1U,     //检测按键按下了
 KEY_TIME,           //检测按键短按状态
 KEY_PRESS_SINGLE,   //检测单次按键
 KEY_PRESS_MULTIPLE, //检测多次按键
} KeyCheckState;

typedef struct KEY_T
{
	  struct KEY_T *Next;        //指向下一个按键
	  GPIO_TypeDef *GPIO;        //按键引脚端口
	  uint16_t      GPIO_Pin;    //按键引脚	
	
		uint8_t  Num;              //第几个按键	
		GPIO_PinState  keyDefaultState;  //默认状态
	  KeyCheckState State;     //按键检测状态
	  uint8_t WantToRead;      //期待能够读取到的按键结果
    uint8_t  Result;         //按键结果
	
    uint16_t SingleCount;    //单击计数
	  uint16_t DoubleCount;    //双击计数
	
	  uint8_t ShortMax;    //短按阈值
	  uint8_t LongMax;     //长按阈值
	  uint8_t MulTap;      //多次按键间隔
	  uint8_t DoubleMax;   //双击阈值
} Key_t;

uint8_t KeyAdd(GPIO_TypeDef *GPIOx,  uint16_t GPIO_Pin, GPIO_PinState  ykeyDefaultState, uint8_t yWantToRead);
void KeyScan(void const * argument);
KeyState KeyOnlyRead(uint8_t yKeyIndex, uint8_t yResult);
KeyState KeyRead(uint8_t yKeyIndex, uint8_t yResult);
__weak  void KeyCallback(Key_t *yKey);

#endif
